<template>
  <!-- <p>这是健康信息填报页面</p> -->
  <el-form ref="form" label-width="100px" :model="reportInfo">
    <el-row>
      <el-form-item label="姓名"
        ><el-col> <el-input v-model="reportInfo.name" style="width:600px"></el-input> </el-col>
      </el-form-item>

      <el-form-item label="手机号"
        ><el-col> <el-input v-model="reportInfo.phoneNum" style="width:600px"></el-input></el-col>
      </el-form-item>

      <el-form-item label="体温"
        ><el-col>
          <el-input v-model="reportInfo.temperature" style="width:600px"></el-input
        ></el-col>
      </el-form-item>

      <el-form-item label="当前所在地"
        ><el-col> <el-input v-model="healthState.address" style="width:600px"></el-input> </el-col>
      </el-form-item>

      <el-form-item label="当前日期"
        ><el-col>
          <el-date-picker
            v-model="reportInfo.date"
            type="date"
            :picker-options="pickerOptions"
            placeholder="请选择"
            readonly
          ></el-date-picker>
        </el-col>
      </el-form-item>

      <el-form-item label-position="top" label="风险自查">
        <el-checkbox-group v-model="checkList">
          <el-col>
            <el-checkbox label="risk">14天内核酸检测为阳性</el-checkbox>
          </el-col>

          <el-col
            ><el-checkbox label="trip"
              >14天内途径中高风险地区</el-checkbox
            ></el-col
          >
          <el-col
            ><el-checkbox label="contactor"
              >14天内接触过中高风险人员</el-checkbox
            ></el-col
          >
        </el-checkbox-group>
      </el-form-item>

      <el-form-item>
        <el-col>
          <el-button type="primary" @click="clickAdd">提交</el-button>
          <el-button @click="cancel">取消</el-button></el-col
        >
      </el-form-item>
    </el-row>
  </el-form>
</template>

<script>
import {
  addReportInfo,
  getHealthState,
  updateHealthState,
} from "../../../api/report";
export default {
  data() {
    return {
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() != Date.now();
        },
      },
      healthState: {},
      reportInfo: {
        address: "",
        phoneNum: "",
        name: "",
        temperature: "",
        date: "",
        trip: 0,
        contactor: 0,
        risk: 0,
      },
      checkList: [],
    };
  },
  created() {
    this.reportInfo.phoneNum = this.$store.state.user.userPhone;
    this.reportInfo.name = this.$store.state.user.username;
    this.getNowTime();
    this.getState();
  },
  methods: {
    getNowTime() {
      var now = new Date();
      var year = now.getFullYear(); //得到年份
      var month = now.getMonth(); //得到月份
      var date = now.getDate(); //得到日期
      month = month + 1;
      month = month.toString().padStart(2, "0");
      date = date.toString().padStart(2, "0");
      var defaultDate = `${year}-${month}-${date}`;
      this.reportInfo.date = defaultDate;
    },
    getState() {
      getHealthState()
        .then((res) => {
          this.healthState = res;
        })
        .catch((error) => console.log(error));
    },
    clickAdd() {
      this.$confirm("确认提交？")
        .then((_) => {
          this.addReport();
        })
        .catch((_) => {});
    },
    addReport() {
      for (let i = 0; i < this.checkList.length; i++) {
        if (this.checkList[i] == "risk") {
          this.reportInfo.risk = 1;
        }
        if (this.checkList[i] == "trip") {
          this.reportInfo.contactor = 1;
        }
        if (this.checkList[i] == "contactor") {
          this.reportInfo.contactor = 1;
        }
      }

      var data1 = JSON.stringify(this.healthState);
      console.log(data1);
      updateHealthState(data1)
        .then((res) => {})
        .catch((error) => console.log(error));

      var data2 = JSON.stringify(this.reportInfo);
      addReportInfo(data2)
        .then((res) => {
          this.$message({
            type: "success",
            message: "填报成功!",
          });
          this.goToHistoryReport();
        })
        .catch((error) => console.log(error));
    },
    goToHistoryReport() {
      this.$router.push("/report/historyHealthState");
    },
    cancel() {
      this.$confirm("确认退出？")
        .then((_) => {
          this.$router.push("/report");
        })
        .catch((_) => {});
    },
  },
};
</script>

<style lang="scss" scoped>
</style>